﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fasterflect;

namespace Preactor.ObjectModel.Entities
{
    /// <summary>
    /// This Cache configuration will store the property names for each record type that should be cached.
    /// </summary>
    public class PropertiesCacheConfiguration
    {
        public PropertiesCacheConfiguration()
        {
            CachedPropertyNamesByType = new Dictionary<Type, IList<string>>();
        }

        private Dictionary<Type, IList<string>> CachedPropertyNamesByType { get; set; }


        public IEnumerable<string> GetCachedPropertyNames(Type type)
        {
            if (CachedPropertyNamesByType.ContainsKey(type))
            {
                return CachedPropertyNamesByType[type];
            }

            return Enumerable.Empty<string>();
        }

        public void SetCachedPropertyNames(Type type, IList<string> propertyNames)
        {
            foreach (var propertyName in propertyNames)
            {
                if (type.Property(propertyName) != null)
                {
                    CachedPropertyNamesByType[type] = propertyNames;
                }
                else
                {
                    throw new ArgumentException(string.Format("The property {0} does not exist in the type for: {1}", propertyName, type.FullName));
                }
            }
        }
    }
}
